package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.SearchResultModel;
import com.banshuibao.bsb_android.util.SearchKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2017/12/16.
 */

public class SearchAdapter extends BaseAdapter {
    private Context context;
    private String key;
    private List<SearchResultModel.DataBean>  list ;

    public SearchAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    public void setKey(String key){
        this.key = key;
    }


    /**
     * 数据至空
     */
    public void clearAdapter(){
        if(this.list!=null)
        list.clear();
        notifyDataSetChanged();
    }

    /**
     * 下拉
     * @return
     */
    public void downData(List<SearchResultModel.DataBean> list){
        if(this.list!=null) {
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }

    }

    /**
     * 上拉数据
     * @return
     */
    public void upData(List<SearchResultModel.DataBean> data){
        if(this.list!=null) {
            this.list.addAll(data);
            notifyDataSetChanged();
        }
    }
    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public SearchResultModel.DataBean getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view == null){
            view = View.inflate(context, R.layout.search_result_item_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.title = view.findViewById(R.id.title);
            view.setTag(viewHolder);
        }else{
           viewHolder = (ViewHolder) view.getTag();
        }
        String s = SearchKey.matcherSearchTitle(list.get(i).getTitle(), key);//关键字
        viewHolder.title.setText(Html.fromHtml(s));
        return view;
    }

    class ViewHolder{
        TextView title;
    }
}
